#include <page.h>
#include <stdio.h>
#include <config.h>
#include <ui.h>
#include <math.h>
#include <input_manager.h>

#define GAP_X 5
#define GAP_Y 5

static Button g_atButton[ITEM_CFG_MAX_NUM];
static int g_iButtoncnt;

static void GenerateButtons(void)
{
    int width, height; // 宽度高度
    int n_per_line ; // 一行几个
    int rows ; // 行数
    int row , col ;// 行列
    int n ;  // 按键个数  
    int i = 0; // 按键的计数值
    int xres, yres; // 屏幕的xy轴长度
    int start_x, start_y; // 案件开始的x，y坐标
    int pre_start_x, pre_start_y; //
    PDispBuffer ptDispBuffer ;// 显示的buffer
    PButton ptButton ;// 显示的按键

    /* 算出单个按钮的width/height */
    n = CountConfigNums();
    g_iButtoncnt = n ;
    ptDispBuffer = GetDispBuffer ();
    xres = ptDispBuffer->iXres ;
    yres = ptDispBuffer->iYres ;
    width = sqrt(1.0 /0.618 *xres * yres / n );
    n_per_line = xres / width + 1 ;
    width = xres / n_per_line;
    height = width * 0.618;

    /* 居中显示：计算每个按钮的region */
    start_x =(xres - width * n_per_line) / 2;
    rows = n / n_per_line ;
    if(rows * n_per_line < n)
        rows++;
    start_y = (yres - rows * height) / 2 ;

    for(row = 0 ; (row < rows) && (i<n) ; row++)
    {
        pre_start_x = start_x - width ;
        pre_start_y = start_y + height * row ;
        for(col = 0 ; (col < n_per_line) && (i<n) ; col++)
        {
            ptButton = &g_atButton[i];
            ptButton->tRegion.iLeftUpX = pre_start_x + width ;
            ptButton->tRegion.iLeftUpY =  pre_start_y ;
            ptButton->tRegion.iHeigh = height - GAP_X;
            ptButton->tRegion.iWidth = width - GAP_Y;
            pre_start_x = ptButton->tRegion.iLeftUpX;

            /* 初始化Button */
            ButtonInit (ptButton , GetConfigByIndex(i)->name ,NULL , NULL , NULL);
            i++;
        }
    }


    /* 绘制Button */

    for(i = 0; i < n; i++)
    {
        g_atButton[i].OnDraw(&g_atButton[i] , ptDispBuffer) ;
    }
}

static int IsTouchInRegion (int iX , int iY , PRegion ptRegion)
{
    if(iX < ptRegion->iLeftUpX || iX >= ptRegion->iLeftUpX + ptRegion->iWidth)
        return 0 ;
    if(iY < ptRegion->iLeftUpY || iY >= ptRegion->iLeftUpY + ptRegion->iHeigh)
        return 0 ;
    else    
        return 1 ;
} 

static PButton GetButtonByName (char *name)
{
    int i ;
    for(i = 0 ; i < n ; i++)
    {
        if(strcmp (name, g_atButton[i].name)== 0)
        {
            return &g_atButton[i];
        }
    }
    return NULL;
}


static PButton GetButtonByInputEvent (PInputEvent ptInputEvent)
{
    int i ;
    char name[100];
    if(ptInputEvent->iType == INPUT_TYPE_TOUCH)
    {
        for(i=0; i < g_iButtoncnt; i++)
        {
            if(IsTouchInRegion(ptInputEvent->ix, ptInputEvent->iy ,&g_atButton[i].tRegion))
                return &g_atButton[i];
        }
    }
    else if(ptInputEvent->iType == INPUT_TYPE_NET)
    {
        sscanf(ptInputEvent->str ,"%s" ,name);
        return GetButtonByName(name);
    }
    else
    return NULL;
}

void MainPageRun(void* pParams)
{
    /* 读取配置文件 */
    int err ;
    InputEvent tevent ;
    PButton ptButton ;
    PDispBuffer ptDispBuffer ;
    err = PraseConfigFile ();
    if(err)
        return err;
    /* 根据配置文件生成按钮，页面 */
    GenerateButtons ();
    while(1)
    {
        /* 读取输入事件 */
        err = GetInPutEvent(&tevent) ;
        if(err)
            continue;
        /* 根据输入事件找到按钮 */
        ptButton = GetButtonByInputEvent(&tevent) ;
        if(!ptButton)
            continue;
        /* 调用按钮的OnPress函数 */
        ptButton->OnPress(ptButton , ptDispBuffer , &tevent);
    }
}



static PageAction g_tMainPage = {
    .name = "main_page",
    .Run  = MainPageRun,
};



void MainPageRegister(void)
{
    PageRegisterInList(&g_tMainPage);
}